package com.fu.mr.bluetoothdchat;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import java.util.List;

/**
 * <pre>
 *     author : Mr.Fu
 *     e-mail : 18622268981@163.com
 *     time   : 2020/03/09
 *     desc   :
 *     version: 1.0
 * </pre>
 */

public class ChatListAdapter extends RecyclerView.Adapter<ChatListAdapter.ChatListHolder> {

    public static int LEFT = 0;
    public static int RIGHT = 1;

    private final Context context;
    private final List<ChatListModel> list;
    private final LayoutInflater inflater;

    public ChatListAdapter(Context context, List<ChatListModel> list) {

        this.context = context;
        this.list = list;
        inflater = LayoutInflater.from(this.context);
    }

    @NonNull
    @Override
    public ChatListHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {

        View view = inflater.inflate(R.layout.item_recycler_view_chats, parent, false);
        return new ChatListHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull ChatListHolder holder, int position) {

        ChatListModel chatListModel = this.list.get(position);
        holder.leftTextView.setText(chatListModel.getLeftText());
        holder.rightTextView.setText(chatListModel.getRightText());
        int type = chatListModel.getType();
        if (type == LEFT) {
            holder.rightTextView.setVisibility(View.GONE);
        } else if (type == RIGHT) {
            holder.leftTextView.setVisibility(View.GONE);
        }
    }

    @Override
    public int getItemCount() {

        return this.list != null ? this.list.size() : 0;
    }

    @Override
    public int getItemViewType(int position) {

        return position;
    }

    class ChatListHolder extends RecyclerView.ViewHolder {

        TextView leftTextView;
        TextView rightTextView;

        public ChatListHolder(View itemView) {
            super(itemView);

            leftTextView = itemView.findViewById(R.id.tv_left_text);
            rightTextView = itemView.findViewById(R.id.tv_right_text);
        }
    }
}
